home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / allocate.c next >
Encoding:
C/C++ Source or Header  |  1995-02-27  |  68.2 KB  |  1,472 lines

  1. /*****************************************************************************
  2. *   Dynamic allocation module of "Irit" - the 3d polygonal solid modeller.   *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. *****************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "irit_sm.h"
  10. #include "iritprsr.h"
  11. #include "allocate.h"
  12. #include "attribut.h"
  13. #include "imalloc.h"
  14.  
  15. /* #define DEBUG1            Print more messages in free/allocating. */
  16.  
  17. #define ALLOCATE_NUM    100       /* Number of objects to allocate at once. */
  18.  
  19. typedef enum {
  20.     ALLOC_OTHER,
  21.     ALLOC_VERTEX,
  22.     ALLOC_POLYGON,
  23.     ALLOC_OBJECT
  24. } AllocateStructType;
  25.  
  26. /* Used for fast reallocation of most common object types: */
  27. static IPVertexStruct
  28.     *VertexFreedList = NULL;
  29. static IPPolygonStruct
  30.     *PolygonFreedList = NULL;
  31. static IPObjectStruct
  32.     *ObjectFreedList = NULL;
  33. static int ComputedAllocateNumObj,
  34.     AllocateNumObj = ALLOCATE_NUM;
  35.  
  36. static void IPFreeObjectSlots(IPObjectStruct *PObj);
  37. static void ListObjectRealloc(IPObjectStruct *PObj);
  38. static void IPMallocObjectSlots(IPObjectStruct *PObj);
  39.  
  40. /*****************************************************************************
  41. * DESCRIPTION:                                                               *
  42. * Routine to free the slots of a given object.                     *
  43. *                                                                            *
  44. * PARAMETERS:                                                                *
  45. *   PObj:    To be freed.                                                    *
  46. *                                                                            *
  47. * RETURN VALUE:                                                              *
  48. *   void                                                                     *
  49. *****************************************************************************/
  50. static void IPFreeObjectSlots(IPObjectStruct *PObj)
  51. {
  52.     int Index;
  53.     char Line[LINE_LEN];
  54.     IPObjectStruct *PObjTmp;
  55.  
  56.     if (PObj == NULL)
  57.     return;
  58.  
  59.     switch (PObj -> ObjType) {
  60.     case IP_OBJ_UNDEF:
  61.         break;
  62.     case IP_OBJ_POLY:           /* Free the polygon list. */
  63.         IPFreePolygonList(PObj -> U.Pl);
  64.         break;
  65.     case IP_OBJ_NUMERIC:
  66.     case IP_OBJ_POINT:
  67.     case IP_OBJ_VECTOR:
  68.     case IP_OBJ_PLANE:
  69.     case IP_OBJ_CTLPT:
  70.         break;
  71.     case IP_OBJ_MATRIX:
  72.         IritFree((VoidPtr) PObj -> U.Mat);
  73.         break;
  74.     case IP_OBJ_STRING:
  75.         IritFree((VoidPtr) PObj -> U.Str);
  76.         break;
  77.     case IP_OBJ_LIST_OBJ: /* Need to dereference list elements. */
  78.         for (Index = 0;
  79.          (PObjTmp = ListObjectGet(PObj, Index)) != NULL;
  80.          Index++) {
  81.         if (PObjTmp -> Count-- == 1)
  82.             IPFreeObject(PObjTmp);
  83.         }
  84.         IritFree((VoidPtr) PObj -> U.Lst.PObjList);
  85.         break;
  86.     case IP_OBJ_CURVE:
  87.         CagdCrvFreeList(PObj -> U.Crvs);
  88.         break;
  89.     case IP_OBJ_SURFACE:
  90.         CagdSrfFreeList(PObj -> U.Srfs);
  91.         break;
  92.     case IP_OBJ_TRIMSRF:
  93.         TrimSrfFreeList(PObj -> U.TrimSrfs);
  94.         break;
  95.     case IP_OBJ_TRIVAR:
  96.         TrivTVFreeList(PObj -> U.Trivars);
  97.         break;
  98.     default:         /* Kill the program - something is WRONG! */
  99.         sprintf(Line,
  100.             "IPFree: Attempt to free undefined Object type %d",
  101.                         PObj -> ObjType);
  102.         IritPrsrFatalError(Line);
  103.         break;
  104.     }
  105. }
  106.  
  107. /*****************************************************************************
  108. * DESCRIPTION:                                                               M
  109. * Allocates one Vertex Structure.                              M
  110. *                                                                            *
  111. * PARAMETERS:                                                                M
  112. *   Count:     Entry into new vertex structure.                              M
  113. *   Tags:      Entry into new vertex structure.                              M
  114. *   PAdj:      Entry into new vertex structure.                              M
  115. *   Pnext:     Entry into new vertex structure.                              M
  116. *                                                                            *
  117. * RETURN VALUE:                                                              M
  118. *   IPVertexStruct *:  A new allocated vertex structure.                     M
  119. *                                                                            *
  120. * KEYWORDS:                                                                  M
  121. *   IPAllocVertex, allocation                                                M
  122. *****************************************************************************/
  123. IPVertexStruct *IPAllocVertex(ByteType Count,
  124.                   ByteType Tags,
  125.                   IPPolygonStruct *PAdj,
  126.                   IPVertexStruct *Pnext)
  127. {
  128.     IPVertexStruct *p;
  129.  
  130.     if (VertexFreedList != NULL) {
  131.     p = VertexFreedList;
  132.     VertexFreedList = VertexFreedList -> Pnext;
  133.     }
  134.     else {
  135.     int i;
  136.     IPVertexStruct *V;
  137.  
  138.     /* Allocate AllocateNumObj objects, returns first one as new   */
  139.     /* and chain together the rest of them into the free list.     */
  140.     if (!ComputedAllocateNumObj)
  141.         AllocateNumObj = getenv("IRIT_MALLOC") ? 1 : ALLOCATE_NUM;
  142.     if ((V = (IPVertexStruct *) IritMalloc(sizeof(IPVertexStruct)
  143.                            * AllocateNumObj)) != NULL) {
  144.         for (i = 1; i < AllocateNumObj - 1; i++)
  145.         V[i].Pnext = &V[i + 1];
  146.         V[AllocateNumObj - 1].Pnext = NULL;
  147.         if (AllocateNumObj > 1)
  148.         VertexFreedList = &V[1];
  149.     }
  150.     p = V;
  151.     }
  152.  
  153.     ZAP_MEM(p, sizeof(IPVertexStruct));
  154.     p -> Count = Count;
  155.     p -> Tags = Tags;
  156.     p -> PAdj = PAdj;
  157.     p -> Pnext = Pnext;
  158.  
  159.     AttrResetAttributes(&p -> Attrs);           /* Initialize attributes. */
  160.  
  161.     return p;
  162. }
  163.  
  164. /*****************************************************************************
  165. * DESCRIPTION:                                                               M
  166. * Allocates one Polygon Structure.                         M
  167. *                                                                            *
  168. * PARAMETERS:                                                                M
  169. *   Count:     Entry into new vertex structure.                              M
  170. *   Tags:      Entry into new vertex structure.                              M
  171. *   V:         Entry into new vertex structure.                              M
  172. *   Pnext:     Entry into new vertex structure.                              M
  173. *                                                                            *
  174. * RETURN VALUE:                                                              M
  175. *   IPPolygonStruct *:  A new allocated polygon structure.                   M
  176. *                                                                            *
  177. * KEYWORDS:                                                                  M
  178. *   IPAllacPolygon, allocation                                               M
  179. *****************************************************************************/
  180. IPPolygonStruct *IPAllocPolygon(ByteType Count,
  181.                 ByteType Tags,
  182.                 IPVertexStruct *V,
  183.                 IPPolygonStruct *Pnext)
  184. {
  185.     IPPolygonStruct *p;
  186.  
  187.     if (PolygonFreedList != NULL) {
  188.     p = PolygonFreedList;
  189.     PolygonFreedList = PolygonFreedList -> Pnext;
  190.     }
  191.     else {
  192.     int i;
  193.     IPPolygonStruct *P;
  194.  
  195.     /* Allocate AllocateNumObj objects, returns first one as new   */
  196.     /* and chain together the rest of them into the free list.     */
  197.     if (!ComputedAllocateNumObj)
  198.         AllocateNumObj = getenv("IRIT_MALLOC") ? 1 : ALLOCATE_NUM;
  199.     if ((P = (IPPolygonStruct *) IritMalloc(sizeof(IPPolygonStruct)
  200.                             * AllocateNumObj)) != NULL) {
  201.         for (i = 1; i < AllocateNumObj - 1; i++)
  202.         P[i].Pnext = &P[i + 1];
  203.         P[AllocateNumObj - 1].Pnext = NULL;
  204.         if (AllocateNumObj > 1)
  205.         PolygonFreedList = &P[1];
  206.     }
  207.     p = P;
  208.     }
  209.  
  210.     ZAP_MEM(p, sizeof(IPPolygonStruct));
  211.     p -> Count = Count;
  212.     p -> Tags = Tags;
  213.     p -> PVertex = V;
  214.     p -> Pnext = Pnext;
  215.  
  216.     AttrResetAttributes(&p -> Attrs);           /* Initialize attributes. */
  217.  
  218.     return p;
  219. }
  220.  
  221. /*****************************************************************************
  222. * DESCRIPTION:                                                               M
  223. * Allocates one Object Structure.                         M
  224. *                                                                            *
  225. * PARAMETERS:                                                                M
  226. *   Name:     Name of newly allocated object.                                M
  227. *   ObjType:  Object type of newly allocated object.                         M
  228. *   Pnext:    Entry into new object structure.                               M
  229. *                                                                            *
  230. * RETURN VALUE:                                                              M
  231. *   IPObjectStruct *:   A new allocated object structure.                    M
  232. *                                                                            *
  233. * KEYWORDS:                                                                  M
  234. *   IPAllocObject, allocation                                                M
  235. *****************************************************************************/
  236. IPObjectStruct *IPAllocObject(char *Name,
  237.                   IPObjStructType ObjType,
  238.                   IPObjectStruct *Pnext)
  239. {
  240.     IPObjectStruct *p;
  241.  
  242.     if (ObjectFreedList != NULL) {
  243.     p = ObjectFreedList;
  244.     ObjectFreedList = ObjectFreedList -> Pnext;
  245.     }
  246.     else {
  247.     int i;
  248.     IPObjectStruct *O;
  249.  
  250.     /* Allocate AllocateNumObj objects, returns first one as new   */
  251.     /* and chain together the rest of them into the free list.     */
  252.     if (!ComputedAllocateNumObj)
  253.         AllocateNumObj = getenv("IRIT_MALLOC") ? 1 : ALLOCATE_NUM;
  254.     if ((O = (IPObjectStruct *) IritMalloc(sizeof(IPObjectStruct)
  255.                            * AllocateNumObj)) != NULL) {
  256.         for (i = 1; i < AllocateNumObj - 1; i++)
  257.         O[i].Pnext = &O[i + 1];
  258.         O[AllocateNumObj - 1].Pnext = NULL;
  259.         if (AllocateNumObj > 1)
  260.         ObjectFreedList = &O[1];
  261.     }
  262.     p = O;
  263.     }
  264.  
  265.     ZAP_MEM(p, sizeof(IPObjectStruct));
  266.     strcpy(p -> Name, Name);
  267.     p -> ObjType = ObjType;
  268.     p -> Count = 1;
  269.     p -> Pnext = Pnext;
  270.  
  271.     IPMallocObjectSlots(p);
  272.     AttrResetAttributes(&p -> Attrs);           /* Initialize attributes. */
  273.  
  274.     return p;
  275. }
  276.  
  277. /*****************************************************************************
  278. * DESCRIPTION:                                                               M
  279. * Frees one Vertex Structure.                             M
  280. *                                                                            *
  281. * PARAMETERS:                                                                M
  282. *   V:        To free.                                                       M
  283. *                                                                            *
  284. * RETURN VALUE:                                                              M
  285. *   void                                                                     M
  286. *                                                                            *
  287. * KEYWORDS:                                                                  M
  288. *   IPFreeVertex, allocation                                                 M
  289. *****************************************************************************/
  290. void IPFreeVertex(IPVertexStruct *V)
  291. {
  292.     if (V != NULL) {
  293.     V ->Pnext = NULL;
  294.     IPFreeVertexList(V);
  295.     }
  296. }
  297.  
  298. /*****************************************************************************
  299. * DESCRIPTION:                                                               M
  300. * Frees one Polygon Structure.                             M
  301. *                                                                            *
  302. * PARAMETERS:                                                                M
  303. *   P:        To free.                                                       M
  304. *                                                                            *
  305. * RETURN VALUE:                                                              M
  306. *   void                                                                     M
  307. *                                                                            *
  308. * KEYWORDS:                                                                  M
  309. *   IPFreePolygon, allocation                                                M
  310. *****************************************************************************/
  311. void IPFreePolygon(IPPolygonStruct *P)
  312. {
  313.     if (P != NULL) {
  314.     P -> Pnext = NULL;
  315.     IPFreePolygonList(P);
  316.     }
  317. }
  318.  
  319. /*****************************************************************************
  320. * DESCRIPTION:                                                               M
  321. * Frees one Object Structure.                             M
  322. *                                                                            *
  323. * PARAMETERS:                                                                M
  324. *   O:        To free.                                                       M
  325. *                                                                            *
  326. * RETURN VALUE:                                                              M
  327. *   void                                                                     M
  328. *                                                                            *
  329. * KEYWORDS:                                                                  M
  330. *   IPFreeObject, allocation                                                 M
  331. *****************************************************************************/
  332. void IPFreeObject(IPObjectStruct *O)
  333. {
  334.     if (O != NULL) {
  335.     if (O -> Count > 1) {
  336.         /* Do not free object - just decrease its reference count. */
  337.         O -> Count--;
  338.     }
  339.     else {
  340.         AttrFreeAttributes(&O -> Attrs);
  341.         IPFreeObjectSlots(O);
  342.  
  343.         /* Add it to global freed object list: */
  344.         O -> Pnext = ObjectFreedList;
  345.         ObjectFreedList = O;
  346.     }
  347.     }
  348. }
  349.  
  350. /*****************************************************************************
  351. * DESCRIPTION:                                                               M
  352. * Free a, possibly circular, list of Vertex structures.                      M
  353. *                                                                            *
  354. * PARAMETERS:                                                                M
  355. *   VFirst:   To free.                                                       M
  356. *                                                                            *
  357. * RETURN VALUE:                                                              M
  358. *   void                                                                     M
  359. *                                                                            *
  360. * KEYWORDS:                                                                  M
  361. *   IPFreeVertexList, allocation                                             M
  362. *****************************************************************************/
  363. void IPFreeVertexList(IPVertexStruct *VFirst)
  364. {
  365.     if (VFirst != NULL) {
  366.     IPVertexStruct *Vtemp,
  367.         *V = VFirst;
  368.  
  369.     /* Handle both circular or NULL terminated. */
  370.     do {
  371.         Vtemp = V;
  372.         V = V -> Pnext;
  373.     }
  374.     while (V != NULL && V != VFirst);
  375.  
  376.     /* Now chain this new list to the global freed vertex list: */
  377.     Vtemp -> Pnext = VertexFreedList;
  378.     VertexFreedList = VFirst;
  379.     }
  380. }
  381.  
  382. /*****************************************************************************
  383. * DESCRIPTION:                                                               M
  384. * Free a list of Polygon structures.                                 M
  385. *                                                                            *
  386. * PARAMETERS:                                                                M
  387. *   PFirst:   To free.                                                       M
  388. *                                                                            *
  389. * RETURN VALUE:                                                              M
  390. *   void                                                                     M
  391. *                                                                            *
  392. * KEYWORDS:                                                                  M
  393. *   IPFreePolygonList, allocation                                            M
  394. *****************************************************************************/
  395. void IPFreePolygonList(IPPolygonStruct *PFirst)
  396. {
  397.     if (PFirst != NULL) {
  398.     IPPolygonStruct
  399.         *Ptemp = NULL,
  400.         *P = PFirst;
  401.  
  402.     while (P != NULL) {
  403.         IPFreeVertexList(P -> PVertex);
  404.  
  405.         Ptemp = P;
  406.         P = P -> Pnext;
  407.     }
  408.  
  409.     /* Now chain this new list to the global freed polygon list: */
  410.     Ptemp -> Pnext = PolygonFreedList;
  411.     PolygonFreedList = PFirst;
  412.     }
  413. }
  414.  
  415. /*****************************************************************************
  416. * DESCRIPTION:                                                               M
  417. * Free a list of Object structures.                                 M
  418. *                                                                            *
  419. * PARAMETERS:                                                                M
  420. *   OFirst:   To free.                                                       M
  421. *                                                                            *
  422. * RETURN VALUE:                                                              M
  423. *   void                                                                     M
  424. *                                                                            *
  425. * KEYWORDS:                                                                  M
  426. *   IPFreeObjectList, allocation                                             M
  427. *****************************************************************************/
  428. void IPFreeObjectList(IPObjectStruct *OFirst)
  429. {
  430.     while (OFirst != NULL) {
  431.     IPObjectStruct
  432.         *NextO = OFirst -> Pnext;
  433.  
  434.     IPFreeObject(OFirst);
  435.     OFirst = NextO;
  436.     }
  437. }
  438.  
  439. /*****************************************************************************
  440. * DESCRIPTION:                                                               M
  441. * Returns the length of a list, given a list of objects.             M
  442. *                                                                            *
  443. * PARAMETERS:                                                                M
  444. *   PObj:      A list of objects to find its length.                         M
  445. *                                                                            *
  446. * RETURN VALUE:                                                              M
  447. *   int:       Resulting length of list PObj.                                M
  448. *                                                                            *
  449. * KEYWORDS:                                                                  M
  450. *   ListObjectLength, linked lists, length                                   M
  451. *****************************************************************************/
  452. int ListObjectLength(IPObjectStruct *PObj)
  453. {
  454.     int i;
  455.     IPObjectStruct **PObjList;
  456.  
  457.     if (!IP_IS_OLST_OBJ(PObj))
  458.     IritPrsrFatalError("List object expected");
  459.  
  460.     for (i = 0, PObjList = PObj -> U.Lst.PObjList; *PObjList++ != NULL; i++)
  461.     if (i >= PObj -> U.Lst.ListMaxLen)
  462.         break;
  463.  
  464.     return i;
  465. }
  466.  
  467. /*****************************************************************************
  468. * DESCRIPTION:                                                               M
  469. * Returns TRUE if PObj is an object in list PObjList or in a sublist of      M
  470. * PObjList, recursively.                                                     M
  471. *                                                                            *
  472. * PARAMETERS:                                                                M
  473. *   PObjList:  To search for PObj in.                                        M
  474. *   PObj:      The element to search in PObjList.                            M
  475. *                                                                            *
  476. * RETURN VALUE:                                                              M
  477. *   int:       TRUE if PObj was found in PObjList, FALSE otherwise.          M
  478. *                                                                            *
  479. * KEYWORDS:                                                                  M
  480. *   ListObjectFind, linked lists, find                                       M
  481. *****************************************************************************/
  482. int ListObjectFind(IPObjectStruct *PObjList, IPObjectStruct *PObj)
  483. {
  484.     IPObjectStruct **PObjSubList;
  485.  
  486.     if (PObjList == PObj)
  487.     return TRUE;
  488.  
  489.     if (!IP_IS_OLST_OBJ(PObjList))
  490.     return FALSE;
  491.  
  492.     for (PObjSubList = PObjList -> U.Lst.PObjList;
  493.      *PObjSubList != NULL;
  494.      PObjSubList++)
  495.     if (ListObjectFind(*PObjSubList, PObj))
  496.         return TRUE;
  497.  
  498.     return FALSE;
  499. }
  500.  
  501. /*****************************************************************************
  502. * DESCRIPTION:                                                               M
  503. * Insert an object PObjItem at index Index into a list of object,PObj.       M
  504. *                                                                            *
  505. * PARAMETERS:                                                                M
  506. *   PObj:       A list of objects to insert PObjItem into.                   M
  507. *   Index:      Index where PObjItem should enter PObj.                      M
  508. *   PObjItem:   Eleent to insert into the list PObj.                         M
  509. *                                                                            *
  510. * RETURN VALUE:                                                              M
  511. *   void                                                                     M
  512. *                                                                            *
  513. * KEYWORDS:                                                                  M
  514. *   ListObjectInsert, lists, insert                                          M
  515. *****************************************************************************/
  516. void ListObjectInsert(IPObjectStruct *PObj,
  517.               int Index,
  518.               IPObjectStruct *PObjItem)
  519. {
  520.     if (!IP_IS_OLST_OBJ(PObj))
  521.     IritPrsrFatalError("List object expected");
  522.  
  523.     if (PObj -> U.Lst.ListMaxLen <= Index)
  524.     ListObjectRealloc(PObj);
  525.  
  526.     PObj -> U.Lst.PObjList[Index] = PObjItem;
  527. }
  528.  
  529. /*****************************************************************************
  530. * DESCRIPTION:                                                               M
  531. * Returns the object number Index in list of PObjList object.             M
  532. *                                                                            M
  533. *                                                                            *
  534. * PARAMETERS:                                                                M
  535. *   PObj:     A list object to extract one object from.                      M
  536. *   Index:    Index of object to extractfrom PObj.                           M
  537. *                                                                            *
  538. * RETURN VALUE:                                                              M
  539. *   IPObjectStruct *:  Index object in list PObj, or NULL if no such thing.  M
  540. *                                                                            *
  541. * KEYWORDS:                                                                  M
  542. *   ListObjectGet, lists, find                                               M
  543. *****************************************************************************/
  544. IPObjectStruct *ListObjectGet(IPObjectStruct *PObj, int Index)
  545. {
  546.     if (!IP_IS_OLST_OBJ(PObj))
  547.     IritPrsrFatalError("List object expected");
  548.  
  549.     return PObj -> U.Lst.ListMaxLen > Index ? PObj -> U.Lst.PObjList[Index]
  550.                         : NULL;
  551. }
  552.  
  553. /*****************************************************************************
  554. * DESCRIPTION:                                                               *
  555. * Reallocate a list Object to twice the previous list size             *
  556. *                                                                            *
  557. * PARAMETERS:                                                                *
  558. *   PObj:      A list object to reallocate.                                  *
  559. *                                                                            *
  560. * RETURN VALUE:                                                              *
  561. *   void                                                                     *
  562. *****************************************************************************/
  563. static void ListObjectRealloc(IPObjectStruct *PObj)
  564. {
  565.     IPObjectStruct
  566.     **PObjList = IritMalloc(sizeof(IPObjectStruct *) *
  567.                 PObj -> U.Lst.ListMaxLen * 2);
  568.  
  569.     GEN_COPY(PObjList, PObj -> U.Lst.PObjList,
  570.          PObj -> U.Lst.ListMaxLen * sizeof(IPObjectStruct *));
  571.     PObj -> U.Lst.ListMaxLen *= 2;
  572.     IritFree((VoidPtr) PObj -> U.Lst.PObjList);
  573.     PObj -> U.Lst.PObjList = PObjList;
  574. }
  575.  
  576. /*****************************************************************************
  577. * DESCRIPTION:                                                               *
  578. * Routine to initialize the slots of a given object.                 *
  579. *                                                                            *
  580. * PARAMETERS:                                                                *
  581. *   PObj:     Allocated object to initialize.                                *
  582. *                                                                            *
  583. * RETURN VALUE:                                                              *
  584. *   void                                                                     *
  585. *****************************************************************************/
  586. static void IPMallocObjectSlots(IPObjectStruct *PObj)
  587. {
  588.     switch (PObj -> ObjType) {
  589.     case IP_OBJ_MATRIX:
  590.         PObj -> U.Mat = (MatrixType *) IritMalloc(sizeof(MatrixType));
  591.         break;
  592.     case IP_OBJ_STRING:
  593.         PObj -> U.Str = (char *) IritMalloc(LINE_LEN);
  594.         PObj -> U.Str[0] = 0;
  595.         break;
  596.     case IP_OBJ_LIST_OBJ:
  597.         PObj -> U.Lst.PObjList = (IPObjectStruct **)
  598.         IritMalloc(sizeof(IPObjectStruct *) * MAX_OBJ_LIST);
  599.         PObj -> U.Lst.PObjList[0] = NULL;
  600.         PObj -> U.Lst.ListMaxLen = MAX_OBJ_LIST;
  601.         break;
  602.     default:
  603.         PObj -> U.VPtr = NULL;
  604.         break;
  605.     }
  606. }
  607.  
  608. /*****************************************************************************
  609. * DESCRIPTION:                                                               M
  610. * Creates one polygonal object.                             M
  611. *                                                                            *
  612. * PARAMETERS:                                                                M
  613. *   Name:     Name of polygonal object.                                      M
  614. *   Pl:       Polygon(s) to place in object.                                 M
  615. *   Pnext:    Entry into the object structure.                               M
  616. *                                                                            *
  617. * RETURN VALUE:                                                              M
  618. *   IPObjectStruct *:   A newly created polygonal object.                    M
  619. *                                                                            *
  620. * KEYWORDS:                                                                  M
  621. *   GenPolyObject, allocation                                                M
  622. *****************************************************************************/
  623. IPObjectStruct *GenPolyObject(char *Name,
  624.                   IPPolygonStruct *Pl,
  625.                   IPObjectStruct *Pnext)
  626. {
  627.     IPObjectStruct *PObj;
  628.  
  629.     PObj = IPAllocObject(Name, IP_OBJ_POLY, Pnext);
  630.     IP_SET_POLYGON_OBJ(PObj);           /* Default - not polyline object. */
  631.  
  632.     PObj -> U.Pl = Pl;                 /* Link the union part of it... */
  633.  
  634.     return PObj;
  635. }
  636.  
  637. /*****************************************************************************
  638. * DESCRIPTION:                                                               M
  639. * Creates one polygonal object.                             M
  640. *                                                                            *
  641. * PARAMETERS:                                                                M
  642. *   Pl:       Polygon(s) to place in object.                                 M
  643. *                                                                            *
  644. * RETURN VALUE:                                                              M
  645. *   IPObjectStruct *:   A newly created polygonal object.                    M
  646. *                                                                            *
  647. * KEYWORDS:                                                                  M
  648. *   GenPOLYObject, allocation                                                M
  649. *****************************************************************************/
  650. IPObjectStruct *GenPOLYObject(IPPolygonStruct *Pl)
  651. {
  652.     return GenPolyObject("", Pl, NULL);
  653. }
  654.  
  655. /*****************************************************************************
  656. * DESCRIPTION:                                                               M
  657. * Creates one curve object.                             M
  658. *                                                                            *
  659. * PARAMETERS:                                                                M
  660. *   Name:     Name of polygonal object.                                      M
  661. *   Crv:      Curves to place in object.                                     M
  662. *   Pnext:    Entry into the object structure.                               M
  663. *                                                                            *
  664. * RETURN VALUE:                                                              M
  665. *   IPObjectStruct *:   A newly created curve object.                        M
  666. *                                                                            *
  667. * KEYWORDS:                                                                  M
  668. *   GenCrvObject, allocation                                                 M
  669. *****************************************************************************/
  670. IPObjectStruct *GenCrvObject(char *Name,
  671.                  CagdCrvStruct *Crv,
  672.                  IPObjectStruct *Pnext)
  673. {
  674.     IPObjectStruct *PObj;
  675.  
  676.     PObj = IPAllocObject(Name, IP_OBJ_CURVE, Pnext);
  677.  
  678.     PObj -> U.Crvs = Crv;             /* Link the union part of it... */
  679.  
  680.     return PObj;
  681. }
  682.  
  683. /*****************************************************************************
  684. * DESCRIPTION:                                                               M
  685. * Creates one curve object.                             M
  686. *                                                                            *
  687. * PARAMETERS:                                                                M
  688. *   Crv:      Curves to place in object.                                     M
  689. *                                                                            *
  690. * RETURN VALUE:                                                              M
  691. *   IPObjectStruct *:   A newly created curve object.                        M
  692. *                                                                            *
  693. * KEYWORDS:                                                                  M
  694. *   GenCRVObject, allocation                                                 M
  695. *****************************************************************************/
  696. IPObjectStruct *GenCRVObject(CagdCrvStruct *Crv)
  697. {
  698.     return GenCrvObject("", Crv, NULL);
  699. }
  700.  
  701. /*****************************************************************************
  702. * DESCRIPTION:                                                               M
  703. * Creates one surface object.                             M
  704. *                                                                            *
  705. * PARAMETERS:                                                                M
  706. *   Name:     Name of polygonal object.                                      M
  707. *   Srf:      Surfaces to place in object.                                   M
  708. *   Pnext:    Entry into the object structure.                               M
  709. *                                                                            *
  710. * RETURN VALUE:                                                              M
  711. *   IPObjectStruct *:   A newly created surface object.                      M
  712. *                                                                            *
  713. * KEYWORDS:                                                                  M
  714. *   GenSrfObject, allocation                                                 M
  715. *****************************************************************************/
  716. IPObjectStruct *GenSrfObject(char *Name,
  717.                  CagdSrfStruct *Srf,
  718.                  IPObjectStruct *Pnext)
  719. {
  720.     IPObjectStruct *PObj;
  721.  
  722.     PObj = IPAllocObject(Name, IP_OBJ_SURFACE, Pnext);
  723.  
  724.     PObj -> U.Srfs = Srf;             /* Link the union part of it... */
  725.  
  726.     return PObj;
  727. }
  728.  
  729. /*****************************************************************************
  730. * DESCRIPTION:                                                               M
  731. * Creates one surface object.                             M
  732. *                                                                            *
  733. * PARAMETERS:                                                                M
  734. *   Srf:      Surfaces to place in object.                                   M
  735. *                                                                            *
  736. * RETURN VALUE:                                                              M
  737. *   IPObjectStruct *:   A newly created surface object.                      M
  738. *                                                                            *
  739. * KEYWORDS:                                                                  M
  740. *   GenSRFObject, allocation                                                 M
  741. *****************************************************************************/
  742. IPObjectStruct *GenSRFObject(CagdSrfStruct *Srf)
  743. {
  744.     return GenSrfObject("", Srf, NULL);
  745. }
  746.  
  747. /*****************************************************************************
  748. * DESCRIPTION:                                                               M
  749. * Creates one surface object.                             M
  750. *                                                                            *
  751. * PARAMETERS:                                                                M
  752. *   Name:     Name of polygonal object.                                      M
  753. *   TrimSrf:  Trimmed surfaces to place in object.                           M
  754. *   Pnext:    Entry into the object structure.                               M
  755. *                                                                            *
  756. * RETURN VALUE:                                                              M
  757. *   IPObjectStruct *:   A newly created trimmed surface object.              M
  758. *                                                                            *
  759. * KEYWORDS:                                                                  M
  760. *   GenTrimSrfObject, allocation                                             M
  761. *****************************************************************************/
  762. IPObjectStruct *GenTrimSrfObject(char *Name,
  763.                  TrimSrfStruct *TrimSrf,
  764.                  IPObjectStruct *Pnext)
  765. {
  766.     IPObjectStruct *PObj;
  767.  
  768.     PObj = IPAllocObject(Name, IP_OBJ_TRIMSRF, Pnext);
  769.  
  770.     PObj -> U.TrimSrfs = TrimSrf;        /* Link the union part of it... */
  771.  
  772.     return PObj;
  773. }
  774.  
  775. /*****************************************************************************
  776. * DESCRIPTION:                                                               M
  777. * Creates one surface object.                             M
  778. *                                                                            *
  779. * PARAMETERS:                                                                M
  780. *   TrimSrf:      Trimmed surfaces to place in object.                       M
  781. *                                                                            *
  782. * RETURN VALUE:                                                              M
  783. *   IPObjectStruct *:   A newly created trimmed surface object.              M
  784. *                                                                            *
  785. * KEYWORDS:                                                                  M
  786. *   GenTRIMSRFObject, allocation                                             M
  787. *****************************************************************************/
  788. IPObjectStruct *GenTRIMSRFObject(TrimSrfStruct *TrimSrf)
  789. {
  790.     return GenTrimSrfObject("", TrimSrf, NULL);
  791. }
  792.  
  793. /*****************************************************************************
  794. * DESCRIPTION:                                                               M
  795. * Creates one trivariate object.                         M
  796. *                                                                            *
  797. * PARAMETERS:                                                                M
  798. *   Name:     Name of polygonal object.                                      M
  799. *   Triv:     Trivariates to place in object.                                M
  800. *   Pnext:    Entry into the object structure.                               M
  801. *                                                                            *
  802. * RETURN VALUE:                                                              M
  803. *   IPObjectStruct *:   A newly created trivariate object.                   M
  804. *                                                                            *
  805. * KEYWORDS:                                                                  M
  806. *   GenTrivarObject, allocation                                              M
  807. *****************************************************************************/
  808. IPObjectStruct *GenTrivarObject(char *Name,
  809.                 TrivTVStruct *Triv,
  810.                 IPObjectStruct *Pnext)
  811. {
  812.     IPObjectStruct *PObj;
  813.  
  814.     PObj = IPAllocObject(Name, IP_OBJ_TRIVAR, Pnext);
  815.  
  816.     PObj -> U.Trivars = Triv;             /* Link the union part of it... */
  817.  
  818.     return PObj;
  819. }
  820.  
  821. /*****************************************************************************
  822. * DESCRIPTION:                                                               M
  823. * Creates one trivariate object.                         M
  824. *                                                                            *
  825. * PARAMETERS:                                                                M
  826. *   Triv:      Trivariates to place in object.                               M
  827. *                                                                            *
  828. * RETURN VALUE:                                                              M
  829. *   IPObjectStruct *:   A newly created trivariate object.                   M
  830. *                                                                            *
  831. * KEYWORDS:                                                                  M
  832. *   GenTRIVARObject, allocation                                              M
  833. *****************************************************************************/
  834. IPObjectStruct *GenTRIVARObject(TrivTVStruct *Triv)
  835. {
  836.     return GenTrivarObject("", Triv, NULL);
  837. }
  838.  
  839. /*****************************************************************************
  840. * DESCRIPTION:                                                               M
  841. * Creates one control point object.                         M
  842. *   Only one of CagdCoords/Coords should be specified.                 M
  843. *                                                                            *
  844. * PARAMETERS:                                                                M
  845. *   Name:        Name of polygonal object.                                   M
  846. *   PtType:      Point type of created control point (E2, P3, etc.).         M
  847. *   CagdCoords:  If specified,  used as coefficients of new control point.   M
  848. *   Coords:      If specified,  used as coefficients of new control point.   M
  849. *   Pnext:       Entry into the object structure.                            M
  850. *                                                                            *
  851. * RETURN VALUE:                                                              M
  852. *   IPObjectStruct *:     A newly created control point object.              M
  853. *                                                                            *
  854. * KEYWORDS:                                                                  M
  855. *   GenCtlPtObject, allocation                                               M
  856. *****************************************************************************/
  857. IPObjectStruct *GenCtlPtObject(char *Name,
  858.                    CagdPointType PtType,
  859.                    CagdRType *CagdCoords,
  860.                    RealType *Coords,
  861.                    IPObjectStruct *Pnext)
  862. {
  863.     int i;
  864.     CagdBType
  865.     IsNotRational = !CAGD_IS_RATIONAL_PT(PtType);
  866.     IPObjectStruct *PObj;
  867.     RealType *t;
  868.  
  869.     PObj = IPAllocObject(Name, IP_OBJ_CTLPT, Pnext);
  870.  
  871.     PObj -> U.CtlPt.PtType = PtType;
  872.     t = PObj -> U.CtlPt.Coords;
  873.  
  874.     if (CagdCoords != NULL)
  875.     for (i = IsNotRational; i <= CAGD_NUM_OF_PT_COORD(PtType); i++)
  876.         t[i] = CagdCoords[i];
  877.     else
  878.     for (i = IsNotRational; i <= CAGD_NUM_OF_PT_COORD(PtType); i++)
  879.         t[i] = Coords[i];
  880.  
  881.     return PObj;
  882. }
  883.  
  884. /*****************************************************************************
  885. * DESCRIPTION:                                                               M
  886. * Creates one control point object.                         M
  887. *   Only one of CagdCoords/Coords should be specified.                 M
  888. *                                                                            *
  889. * PARAMETERS:                                                                M
  890. *   PtType:      Point type of created control point (E2, P3, etc.).         M
  891. *   CagdCoords:  If specified,  used as coefficients of new control point.   M
  892. *   Coords:      If specified,  used as coefficients of new control point.   M
  893. *                                                                            *
  894. * RETURN VALUE:                                                              M
  895. *   IPObjectStruct *:     A newly created control point object.              M
  896. *                                                                            *
  897. * KEYWORDS:                                                                  M
  898. *   GenCTLPTObject, allocation                                               M
  899. *****************************************************************************/
  900. IPObjectStruct *GenCTLPTObject(CagdPointType PtType,
  901.                    CagdRType *CagdCoords,
  902.                    RealType *Coords)
  903. {
  904.     return GenCtlPtObject("", PtType, CagdCoords, Coords, NULL);
  905. }
  906.  
  907. /*****************************************************************************
  908. * DESCRIPTION:                                                               M
  909. * Creates one numeric object.                                                M
  910. *                                                                            *
  911. * PARAMETERS:                                                                M
  912. *   Name:     Name of polygonal object.                                      M
  913. *   R:        Numeric value to place in object.                              M
  914. *   Pnext:    Entry into the object structure.                               M
  915. *                                                                            *
  916. * RETURN VALUE:                                                              M
  917. *   IPObjectStruct *:    A newly created numeric object.                     M
  918. *                                                                            *
  919. * KEYWORDS:                                                                  M
  920. *   GenNumObject, allocation                                                 M
  921. *****************************************************************************/
  922. IPObjectStruct *GenNumObject(char *Name, RealType *R, IPObjectStruct *Pnext)
  923. {
  924.     IPObjectStruct *PObj;
  925.  
  926.     PObj = IPAllocObject(Name, IP_OBJ_NUMERIC, Pnext);
  927.  
  928.     PObj -> U.R = *R;                 /* Link the union part of it... */
  929.  
  930.     return PObj;
  931. }
  932.  
  933. /*****************************************************************************
  934. * DESCRIPTION:                                                               M
  935. * Creates one numeric object.                                                M
  936. *                                                                            *
  937. * PARAMETERS:                                                                M
  938. *   R:        Numeric value to place in object.                              M
  939. *                                                                            *
  940. * RETURN VALUE:                                                              M
  941. *   IPObjectStruct *:    A newly created numeric object.                     M
  942. *                                                                            *
  943. * KEYWORDS:                                                                  M
  944. *   GenNUMObject, allocation                                                 M
  945. *****************************************************************************/
  946. IPObjectStruct *GenNUMObject(RealType *R)
  947. {
  948.     return GenNumObject("", R, NULL);
  949. }
  950.  
  951. /*****************************************************************************
  952. * DESCRIPTION:                                                               M
  953. * Creates one numeric object.                                                M
  954. *                                                                            *
  955. * PARAMETERS:                                                                M
  956. *   R:        Numeric value to place in object.                              M
  957. *                                                                            *
  958. * RETURN VALUE:                                                              M
  959. *   IPObjectStruct *:    A newly created numeric object.                     M
  960. *                                                                            *
  961. * KEYWORDS:                                                                  M
  962. *   GenNUMValObject, allocation                                              M
  963. *****************************************************************************/
  964. IPObjectStruct *GenNUMValObject(RealType R)
  965. {
  966.     return GenNumObject("", &R, NULL);
  967. }
  968.  
  969. /*****************************************************************************
  970. * DESCRIPTION:                                                               M
  971. * Creates one point object.                                                  M
  972. *                                                                            *
  973. * PARAMETERS:                                                                M
  974. *   Name:           Name of polygonal object.                                M
  975. *   Pt0, Pt1, Pt2:  Coefficients of point.                                   M
  976. *   Pnext:          Entry into the object structure.                         M
  977. *                                                                            *
  978. * RETURN VALUE:                                                              M
  979. *   IPObjectStruct *:    A newly created point object.                       M
  980. *                                                                            *
  981. * KEYWORDS:                                                                  M
  982. *   GenPtObject, allocation                                                  M
  983. *****************************************************************************/
  984. IPObjectStruct *GenPtObject(char *Name,
  985.                 RealType *Pt0,
  986.                 RealType *Pt1,
  987.                 RealType *Pt2,
  988.                 IPObjectStruct *Pnext)
  989. {
  990.     IPObjectStruct *PObj;
  991.  
  992.     PObj = IPAllocObject(Name, IP_OBJ_POINT, Pnext);
  993.  
  994.     PObj -> U.Pt[0] = *Pt0;             /* Link the union part of it... */
  995.     PObj -> U.Pt[1] = *Pt1;
  996.     PObj -> U.Pt[2] = *Pt2;
  997.  
  998.     return PObj;
  999. }
  1000.  
  1001. /*****************************************************************************
  1002. * DESCRIPTION:                                                               M
  1003. * Creates one point object.                                                  M
  1004. *                                                                            *
  1005. * PARAMETERS:                                                                M
  1006. *   Pt0, Pt1, Pt2:  Coefficients of point.                                   M
  1007. *                                                                            *
  1008. * RETURN VALUE:                                                              M
  1009. *   IPObjectStruct *:    A newly created point object.                       M
  1010. *                                                                            *
  1011. * KEYWORDS:                                                                  M
  1012. *   GenPTObject, allocation                                                  M
  1013. *****************************************************************************/
  1014. IPObjectStruct *GenPTObject(RealType *Pt0, RealType *Pt1, RealType *Pt2)
  1015. {
  1016.     return GenPtObject("", Pt0, Pt1, Pt2, NULL);
  1017. }
  1018.  
  1019. /*****************************************************************************
  1020. * DESCRIPTION:                                                               M
  1021. * Creates one vector object.                                                 M
  1022. *                                                                            *
  1023. * PARAMETERS:                                                                M
  1024. *   Name:              Name of polygonal object.                             M
  1025. *   Vec0, Vec1, Vec2:  Coefficients of vector.                               M
  1026. *   Pnext:             Entry into the object structure.                      M
  1027. *                                                                            *
  1028. * RETURN VALUE:                                                              M
  1029. *   IPObjectStruct *:    A newly created vector object.                      M
  1030. *                                                                            *
  1031. * KEYWORDS:                                                                  M
  1032. *   GenVecObject, allocation                                                 M
  1033. *****************************************************************************/
  1034. IPObjectStruct *GenVecObject(char *Name,
  1035.                  RealType *Vec0,
  1036.                  RealType *Vec1,
  1037.                  RealType *Vec2,
  1038.                  IPObjectStruct *Pnext)
  1039. {
  1040.     IPObjectStruct *PObj;
  1041.  
  1042.     PObj = IPAllocObject(Name, IP_OBJ_VECTOR, Pnext);
  1043.  
  1044.     PObj -> U.Vec[0] = *Vec0;             /* Link the union part of it... */
  1045.     PObj -> U.Vec[1] = *Vec1;
  1046.     PObj -> U.Vec[2] = *Vec2;
  1047.  
  1048.     return PObj;
  1049. }
  1050.  
  1051. /*****************************************************************************
  1052. * DESCRIPTION:                                                               M
  1053. * Creates one vector object.                                                 M
  1054. *                                                                            *
  1055. * PARAMETERS:                                                                M
  1056. *   Vec0, Vec1, Vec2:  Coefficients of vector.                               M
  1057. *                                                                            *
  1058. * RETURN VALUE:                                                              M
  1059. *   IPObjectStruct *:    A newly created vector object.                      M
  1060. *                                                                            *
  1061. * KEYWORDS:                                                                  M
  1062. *   GenVECObject, allocation                                                 M
  1063. *****************************************************************************/
  1064. IPObjectStruct *GenVECObject(RealType *Vec0, RealType *Vec1, RealType *Vec2)
  1065. {
  1066.     return GenVecObject("", Vec0, Vec1, Vec2, NULL);
  1067. }
  1068.  
  1069. /*****************************************************************************
  1070. * DESCRIPTION:                                                               M
  1071. * Creates one string object.                                                 M
  1072. *                                                                            *
  1073. * PARAMETERS:                                                                M
  1074. *   Name:              Name of string object.                                M
  1075. *   Str:    The string.                                                  M
  1076. *   Pnext:             Entry into the object structure.                      M
  1077. *                                                                            *
  1078. * RETURN VALUE:                                                              M
  1079. *   IPObjectStruct *:    A newly created strtor object.                      M
  1080. *                                                                            *
  1081. * KEYWORDS:                                                                  M
  1082. *   GenStrObject, allocation                                                 M
  1083. *****************************************************************************/
  1084. IPObjectStruct *GenStrObject(char *Name, char *Str, IPObjectStruct *Pnext)
  1085. {
  1086.     IPObjectStruct *PObj;
  1087.  
  1088.     PObj = IPAllocObject(Name, IP_OBJ_STRING, Pnext);
  1089.  
  1090.     PObj -> U.Str = strdup(Str);         /* Link the union part of it... */
  1091.  
  1092.     return PObj;
  1093. }
  1094.  
  1095. /*****************************************************************************
  1096. * DESCRIPTION:                                                               M
  1097. * Creates one string object.                                                 M
  1098. *                                                                            *
  1099. * PARAMETERS:                                                                M
  1100. *   Str:    The string.                                                  M
  1101. *                                                                            *
  1102. * RETURN VALUE:                                                              M
  1103. *   IPObjectStruct *:    A newly created strtor object.                      M
  1104. *                                                                            *
  1105. * KEYWORDS:                                                                  M
  1106. *   GenSTRObject, allocation                                                 M
  1107. *****************************************************************************/
  1108. IPObjectStruct *GenSTRObject(char *Str)
  1109. {
  1110.     return GenStrObject("", Str, NULL);
  1111. }
  1112.  
  1113. /*****************************************************************************
  1114. * DESCRIPTION:                                                               M
  1115. * Creates one plane object.                                                  M
  1116. *                                                                            *
  1117. * PARAMETERS:                                                                M
  1118. *   Name:                            Name of polygonal object.               M
  1119. *   Plane0, Plane1, Plane2, Plane3:  Coefficients of point.                  M
  1120. *   Pnext:                           Entry into the object structure.        M
  1121. *                                                                            *
  1122. * RETURN VALUE:                                                              M
  1123. *   IPObjectStruct *:    A newly created plane object.                       M
  1124. *                                                                            *
  1125. * KEYWORDS:                                                                  M
  1126. *   GenPlaneObject, allocation                                               M
  1127. *****************************************************************************/
  1128. IPObjectStruct *GenPlaneObject(char *Name,
  1129.                    RealType *Plane0,
  1130.                    RealType *Plane1,
  1131.                    RealType *Plane2,
  1132.                    RealType *Plane3,
  1133.                    IPObjectStruct *Pnext)
  1134. {
  1135.     IPObjectStruct *PObj;
  1136.  
  1137.     PObj = IPAllocObject(Name, IP_OBJ_PLANE, Pnext);
  1138.  
  1139.     PObj -> U.Plane[0] = *Plane0;        /* Link the union part of it... */
  1140.     PObj -> U.Plane[1] = *Plane1;
  1141.     PObj -> U.Plane[2] = *Plane2;
  1142.     PObj -> U.Plane[3] = *Plane3;
  1143.  
  1144.     return PObj;
  1145. }
  1146.  
  1147. /*****************************************************************************
  1148. * DESCRIPTION:                                                               M
  1149. * Creates one plane object.                                                  M
  1150. *                                                                            *
  1151. * PARAMETERS:                                                                M
  1152. *   Plane0, Plane1, Plane2, Plane3:  Coefficients of point.                  M
  1153. *                                                                            *
  1154. * RETURN VALUE:                                                              M
  1155. *   IPObjectStruct *:    A newly created plane object.                       M
  1156. *                                                                            *
  1157. * KEYWORDS:                                                                  M
  1158. *   GenPLANEObject, allocation                                               M
  1159. *****************************************************************************/
  1160. IPObjectStruct *GenPLANEObject(RealType *Plane0,
  1161.                    RealType *Plane1,
  1162.                    RealType *Plane2,
  1163.                    RealType *Plane3)
  1164. {
  1165.     return GenPlaneObject("", Plane0, Plane1, Plane2, Plane3, NULL);
  1166. }
  1167.  
  1168. /*****************************************************************************
  1169. * DESCRIPTION:                                                               M
  1170. * Creates one matrix object.                                                 M
  1171. *                                                                            *
  1172. * PARAMETERS:                                                                M
  1173. *   Name:     Name of polygonal object.                                     M
  1174. *   Mat:      Matrix to initialize with.                     M
  1175. *   Pnext:    Entry into the object structure.                             M
  1176. *                                                                            *
  1177. * RETURN VALUE:                                                              M
  1178. *   IPObjectStruct *:   A newly created matrix object.                       M
  1179. *                                                                            *
  1180. * KEYWORDS:                                                                  M
  1181. *   GenMatObject, allocation                                                 M
  1182. *****************************************************************************/
  1183. IPObjectStruct *GenMatObject(char *Name, MatrixType Mat, IPObjectStruct *Pnext)
  1184. {
  1185.     int i, j;
  1186.     IPObjectStruct *PObj;
  1187.  
  1188.     PObj = IPAllocObject(Name, IP_OBJ_MATRIX, Pnext);
  1189.  
  1190.     for (i = 0; i < 4; i++)             /* Link the union part of it... */
  1191.     for (j = 0; j < 4; j++)
  1192.         (*PObj -> U.Mat)[i][j] = Mat[i][j];
  1193.  
  1194.     return PObj;
  1195. }
  1196.  
  1197. /*****************************************************************************
  1198. * DESCRIPTION:                                                               M
  1199. * Creates one matrix object.                                                 M
  1200. *                                                                            *
  1201. * PARAMETERS:                                                                M
  1202. *   Mat:      Matrix to initialize with.                     M
  1203. *                                                                            *
  1204. * RETURN VALUE:                                                              M
  1205. *   IPObjectStruct *:   A newly created matrix object.                       M
  1206. *                                                                            *
  1207. * KEYWORDS:                                                                  M
  1208. *   GenMATObject, allocation                                                 M
  1209. *****************************************************************************/
  1210. IPObjectStruct *GenMATObject(MatrixType Mat)
  1211. {
  1212.     return GenMatObject("", Mat, NULL);
  1213. }
  1214.  
  1215. /*****************************************************************************
  1216. * DESCRIPTION:                                                               M
  1217. * Routine to reallocate as necessary and object to a new object type.        M
  1218. *                                                                            *
  1219. * PARAMETERS:                                                                M
  1220. *   PObj:       Object to reallocated as a new object of type ObjType.       M
  1221. *   ObjType:    New type for object PObj.                                    M
  1222. *                                                                            *
  1223. * RETURN VALUE:                                                              M
  1224. *   void                                                                     M
  1225. *                                                                            *
  1226. * KEYWORDS:                                                                  M
  1227. *   ReallocNewTypeObject, allocation                                         M
  1228. *****************************************************************************/
  1229. void ReallocNewTypeObject(IPObjectStruct *PObj, IPObjStructType ObjType)
  1230. {
  1231.     if (PObj -> ObjType == ObjType)
  1232.         return;
  1233.     IPFreeObjectSlots(PObj);
  1234.     PObj -> ObjType = ObjType;
  1235.     IPMallocObjectSlots(PObj);
  1236. }
  1237.  
  1238. /*****************************************************************************
  1239. * DESCRIPTION:                                                               M
  1240. * Routine to create a whole new copy of an object Src into Dest.         M
  1241. *   If Dest is NULL, new object is allocated, otherwise Dest itself is       M
  1242. * updated to hold the new copy.                                              M
  1243. *   If CopyAll then all the record is copied, otherwise, only its invariant  M
  1244. * elements arebeen copied (i.e. no Name/Pnext copying).                 M
  1245. *                                                                            *
  1246. * PARAMETERS:                                                                M
  1247. *   Dest:      Destination object, possibly NULL.                            M
  1248. *   Src:       Source object.                                                M
  1249. *   CopyAll:   Do we want a complete identical copy?                         M
  1250. *                                                                            *
  1251. * RETURN VALUE:                                                              M
  1252. *   IPObjectStruct *:   Duplicate of Src, same as Dest if Dest != NULL.      M
  1253. *                                                                            *
  1254. * KEYWORDS:                                                                  M
  1255. *   CopyObject, copy                                                         M
  1256. *****************************************************************************/
  1257. IPObjectStruct *CopyObject(IPObjectStruct *Dest,
  1258.                IPObjectStruct *Src,
  1259.                int CopyAll)
  1260. {
  1261.     int Index;
  1262.     char Line[LINE_LEN];
  1263.     IPObjectStruct *PObjTmp;
  1264.  
  1265.     if (Dest == Src)
  1266.     return Dest;            /* Called with same object - ignore. */
  1267.     else if (Dest == NULL)
  1268.     Dest = IPAllocObject("", Src -> ObjType, NULL);
  1269.     else {
  1270.     IPFreeObjectSlots(Dest);
  1271.     Dest -> ObjType = Src -> ObjType;
  1272.     IPMallocObjectSlots(Dest);
  1273.     AttrFreeAttributes(&Dest -> Attrs);
  1274.     }
  1275.  
  1276.     if (CopyAll) {
  1277.     strcpy(Dest -> Name, Src -> Name);
  1278.     Dest -> Pnext = Src -> Pnext;     /* Maybe assigning NULL is better!? */
  1279.     }
  1280.  
  1281.     Dest -> Attrs = AttrCopyAttributes(Src -> Attrs);
  1282.  
  1283.     switch (Src -> ObjType) {
  1284.     case IP_OBJ_UNDEF:
  1285.         break;
  1286.     case IP_OBJ_POLY:
  1287.         Dest -> U.Pl = CopyPolygonList(Src -> U.Pl);
  1288.         if (IP_IS_POLYGON_OBJ(Src))
  1289.             IP_SET_POLYGON_OBJ(Dest);
  1290.         else if (IP_IS_POLYLINE_OBJ(Src))
  1291.             IP_SET_POLYLINE_OBJ(Dest);
  1292.         else if (IP_IS_POINTLIST_OBJ(Src))
  1293.             IP_SET_POINTLIST_OBJ(Dest);
  1294.         break;
  1295.     case IP_OBJ_NUMERIC:
  1296.         Dest -> U.R = Src -> U.R;
  1297.         break;
  1298.     case IP_OBJ_POINT:
  1299.         PT_COPY(Dest -> U.Pt, Src -> U.Pt);
  1300.         break;
  1301.     case IP_OBJ_VECTOR:
  1302.         PT_COPY(Dest -> U.Vec, Src -> U.Vec);
  1303.         break;
  1304.     case IP_OBJ_PLANE:
  1305.         PLANE_COPY(Dest -> U.Plane, Src -> U.Plane);
  1306.         break;
  1307.     case IP_OBJ_CTLPT:
  1308.         GEN_COPY(&Dest -> U.CtlPt, &Src -> U.CtlPt,
  1309.              sizeof(CagdCtlPtStruct));
  1310.         break;
  1311.     case IP_OBJ_MATRIX:
  1312.         if (Dest -> U.Mat == NULL)
  1313.         Dest -> U.Mat = (MatrixType *) IritMalloc(sizeof(MatrixType));
  1314.         MAT_COPY(*Dest -> U.Mat, *Src -> U.Mat);
  1315.         break;
  1316.     case IP_OBJ_STRING:
  1317.         if (Dest -> U.Str == NULL)
  1318.         Dest -> U.Str = (char *) IritMalloc(LINE_LEN);
  1319.         strcpy(Dest -> U.Str, Src -> U.Str);
  1320.         break;
  1321.     case IP_OBJ_LIST_OBJ:
  1322.         if (Dest -> U.Lst.PObjList != NULL)
  1323.         IritFree((VoidPtr) Dest -> U.Lst.PObjList);
  1324.         Dest -> U.Lst.PObjList = (IPObjectStruct **)
  1325.         IritMalloc(sizeof(IPObjectStruct *) * Src -> U.Lst.ListMaxLen);
  1326.         Dest -> U.Lst.ListMaxLen = Src -> U.Lst.ListMaxLen;
  1327.  
  1328.         GEN_COPY(Dest -> U.Lst.PObjList, Src -> U.Lst.PObjList,
  1329.              Dest -> U.Lst.ListMaxLen * sizeof(IPObjectStruct *));
  1330.         for (Index = 0;
  1331.          (PObjTmp = ListObjectGet(Dest, Index)) != NULL;
  1332.          Index++)
  1333.         PObjTmp -> Count++;               /* Inc. # of ref. */
  1334.         break;
  1335.     case IP_OBJ_CURVE:
  1336.         Dest -> U.Crvs = CagdCrvCopyList(Src -> U.Crvs);
  1337.         break;
  1338.     case IP_OBJ_SURFACE:
  1339.         Dest -> U.Srfs = CagdSrfCopyList(Src -> U.Srfs);
  1340.         break;
  1341.     case IP_OBJ_TRIMSRF:
  1342.         Dest -> U.TrimSrfs = TrimSrfCopyList(Src -> U.TrimSrfs);
  1343.         break;
  1344.     case IP_OBJ_TRIVAR:
  1345.         Dest -> U.Trivars = TrivTVCopyList(Src -> U.Trivars);
  1346.         break;
  1347.     default:
  1348.         sprintf(Line,
  1349.         "CopyObject Attemp to copy undefined object %s type %d",
  1350.         Src -> Name, Src -> ObjType);
  1351.         IritPrsrFatalError(Line);
  1352.     }
  1353.     return Dest;
  1354. }
  1355.  
  1356. /*****************************************************************************
  1357. * DESCRIPTION:                                                               M
  1358. * Routine to create a new copy of an object list.                      M
  1359. *                                                                            *
  1360. * PARAMETERS:                                                                M
  1361. *   Pobjs:     Source objects.                                               M
  1362. *   CopyAll:   Do we want a complete identical copy?                         M
  1363. *                                                                            *
  1364. * RETURN VALUE:                                                              M
  1365. *   IPObjectStruct *:    Duplicated list of PObjs.                           M
  1366. *                                                                            *
  1367. * KEYWORDS:                                                                  M
  1368. *   CopyObjectList, copy                                                     M
  1369. *****************************************************************************/
  1370. IPObjectStruct *CopyObjectList(IPObjectStruct *PObjs, int CopyAll)
  1371. {
  1372.     IPObjectStruct *PObj,
  1373.     *NewPObjs = NULL,
  1374.     *TailPObj =NULL;
  1375.  
  1376.     for (PObj = PObjs; PObj != NULL; PObj = PObj -> Pnext) {
  1377.     if (NewPObjs == NULL)
  1378.         NewPObjs = TailPObj = CopyObject(NULL, PObj, CopyAll);
  1379.     else {
  1380.         TailPObj -> Pnext = CopyObject(NULL, PObj, CopyAll);
  1381.         TailPObj = TailPObj -> Pnext;
  1382.     }
  1383.     }
  1384.  
  1385.     return NewPObjs;
  1386. }
  1387.  
  1388. /*****************************************************************************
  1389. * DESCRIPTION:                                                               M
  1390. * Routine to create a new copy of an object polygon list.             M
  1391. *                                                                            *
  1392. * PARAMETERS:                                                                M
  1393. *   Src:      A polygon list to copy.                                        M
  1394. *                                                                            *
  1395. * RETURN VALUE:                                                              M
  1396. *   IPPolygonStruct *:   Duplicated list of polygons.                        M
  1397. *                                                                            *
  1398. * KEYWORDS:                                                                  M
  1399. *   CopyPolygonList, copy                                                    M
  1400. *****************************************************************************/
  1401. IPPolygonStruct *CopyPolygonList(IPPolygonStruct *Src)
  1402. {
  1403.     IPPolygonStruct *Phead, *Ptail;
  1404.  
  1405.     if (Src == NULL)
  1406.     return NULL;
  1407.  
  1408.     /* Prepare the header of the new polygon list: */
  1409.     Phead = Ptail = IPAllocPolygon(1, Src -> Tags,
  1410.                    CopyVertexList(Src -> PVertex), NULL);
  1411.     PLANE_COPY(Ptail -> Plane, Src -> Plane);
  1412.     Ptail -> Attrs = AttrCopyAttributes(Src -> Attrs);
  1413.     IP_RST_BBOX_POLY(Ptail);
  1414.     Src = Src -> Pnext;
  1415.  
  1416.     while (Src != NULL) {
  1417.     Ptail -> Pnext = IPAllocPolygon(Src -> Count, Src -> Tags,
  1418.                     CopyVertexList(Src -> PVertex), NULL);
  1419.     Ptail = Ptail -> Pnext;
  1420.     PLANE_COPY(Ptail -> Plane, Src -> Plane);
  1421.     Ptail -> Attrs = AttrCopyAttributes(Src -> Attrs);
  1422.     IP_RST_BBOX_POLY(Ptail);
  1423.     Src = Src -> Pnext;
  1424.     }
  1425.  
  1426.     return Phead;
  1427. }
  1428.  
  1429. /*****************************************************************************
  1430. * DESCRIPTION:                                                               M
  1431. * Routine to create  a new copy of a polygon vertices list.                  M
  1432. *                                                                            *
  1433. * PARAMETERS:                                                                M
  1434. *   Src:       A vertex list to copy.                                        M
  1435. *                                                                            *
  1436. * RETURN VALUE:                                                              M
  1437. *   IPVertexStruct *:  Duplicated list of vertices.                          M
  1438. *                                                                            *
  1439. * KEYWORDS:                                                                  M
  1440. *   CopyVertexList, copy                                                     M
  1441. *****************************************************************************/
  1442. IPVertexStruct *CopyVertexList(IPVertexStruct *Src)
  1443. {
  1444.     IPVertexStruct *Phead, *Ptail,
  1445.     *SrcFirst = Src;
  1446.  
  1447.     if (Src == NULL)
  1448.     return NULL;
  1449.  
  1450.     /* Prepare the header of the new vertex list: */
  1451.     Phead = Ptail = IPAllocVertex(Src -> Count, Src -> Tags, NULL, NULL);
  1452.     PT_COPY(Phead -> Coord, Src -> Coord);
  1453.     PT_COPY(Phead -> Normal, Src -> Normal);
  1454.     Phead -> Attrs = AttrCopyAttributes(Src -> Attrs);
  1455.     Src = Src -> Pnext;
  1456.  
  1457.     while (Src != SrcFirst && Src != NULL) {
  1458.     Ptail -> Pnext = IPAllocVertex(Src -> Count, Src -> Tags, NULL, NULL);
  1459.     Ptail = Ptail -> Pnext;
  1460.     PT_COPY(Ptail -> Coord, Src -> Coord);
  1461.     PT_COPY(Ptail -> Normal, Src -> Normal);
  1462.     Ptail -> Attrs = AttrCopyAttributes(Src -> Attrs);
  1463.  
  1464.     Src = Src -> Pnext;
  1465.     }
  1466.  
  1467.     if (Src == SrcFirst)
  1468.     Ptail -> Pnext = Phead;               /* Make vertex list circular. */
  1469.  
  1470.     return Phead;
  1471. }
  1472.